<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TabView
 *
 * @author Moon
 */
class TabView extends CTabView {
    const CSS_CLASS='riotingTab';

    //put your code here
    private $publishUrl = '';

    public function run() {
        $baseUrl = Yii::getPathOfAlias('application.widgets.RiotingNerds.assets.Tabview');
        $this->publishUrl = Yii::app()->getAssetManager()->publish($baseUrl);
        if ($this->cssFile !== false && $this->cssFile == '') {
            $this->cssFile = $this->publishUrl . '/tabview.css';
        }
        if (!isset($this->htmlOptions['class']))
            $this->htmlOptions['class'] = 'riotingTab';
        return parent::run();
    }

    public function registerClientScript() {
        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile($this->publishUrl . '/tabView.js');
        $id = $this->getId();
        $cs->registerScript('RiotingNerds#TabView#' . $id, "jQuery(\"#{$id}\").yiitab();");

        if ($this->cssFile !== false)
            self::registerCssFile($this->cssFile);
    }

    protected function renderHeader() {
        echo "<ul class=\"tabs\">\n";
        foreach ($this->tabs as $id => $tab) {
            $title = isset($tab['title']) ? $tab['title'] : 'undefined';
            $active = $id === $this->activeTab ? ' class="active"' : '';
            $url = isset($tab['url']) ? $tab['url'] : "#{$id}";
            $ajax = '';
            if (isset($tab['ajax']))
                $ajax = ' ajax="' . (is_array($tab['ajax']) ? CHtml::normalizeUrl($tab['ajax']) : $tab['ajax']) . '"';
            echo "<li><a href=\"{$url}\"{$active}{$ajax}>{$title}</a></li>\n";
        }
        echo "</ul>\n";
    }

    protected function renderBody() {
        foreach ($this->tabs as $id => $tab) {
            $inactive = $id !== $this->activeTab ? ' style="display:none"' : '';
            echo "<div class=\"view\" id=\"{$id}\"{$inactive}>\n";
            if (!isset($tab['ajax'])) {
                if (isset($tab['content']))
                    echo $tab['content'];
                else if (isset($tab['view'])) {
                    if (isset($tab['data'])) {
                        if (is_array($this->viewData))
                            $data = array_merge($this->viewData, $tab['data']);
                        else
                            $data=$tab['data'];
                    }
                    else
                        $data=$this->viewData;
                    $this->getController()->renderPartial($tab['view'], $data);
                }
            }
            echo "</div><!-- {$id} -->\n";
        }
    }

}

?>
